﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using QLNHang.BUS;
using QLNHang.DTO;

namespace QLNHang.GUI
{
    public partial class ucThemVe : UserControl
    {
        public ucThemVe()
        {
            InitializeComponent();
            GridLoad();
            btnSuaVe.Enabled = false;
        }

        private void btnThemVe_Click(object sender, EventArgs e)
        {
            VeDTO ve = new VeDTO();
            ve.DoiTuong = cbxDoiTuong.SelectedItem.ToString().Trim();
            ve.ThoiGian = cbxThoiGian.SelectedItem.ToString().Trim();
            ve.LoaiVe = cbxLoaiVe.SelectedItem.ToString().Trim();
            ve.GiaVe = txtGiaVe.Text.Trim();
            try
            {
                new VeBUS().ThemVe(ve);
                MessageBox.Show("Thêm vé thành công");
                txtGiaVe.Text = "";
                GridLoad();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }

        }

        private void btnSuaVe_Click(object sender, EventArgs e)
        {
            VeDTO ve = new VeDTO();
            ve.MaVe = lbMaVe.Text.Trim();
            ve.DoiTuong = cbxDoiTuong.SelectedItem.ToString().Trim();
            ve.ThoiGian = cbxThoiGian.SelectedItem.ToString().Trim();
            ve.LoaiVe = cbxLoaiVe.SelectedItem.ToString().Trim();
            ve.GiaVe = txtGiaVe.Text.Trim();
            try
            {
                new VeBUS().SuaVe(ve);
                MessageBox.Show("Sửa vé thành công");
                GridLoad();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        private void gvVe_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            gvVe.ClearSelection();
            gvVe.CurrentCell = null;
            
        }

        private void gvVe_MouseClick(object sender, MouseEventArgs e)
        {
            var selectedRow = gvVe.SelectedRows[0];
            btnThemVe.Enabled = false;
            if (selectedRow != null)
            {
                btnSuaVe.Enabled = true;
                cbxDoiTuong.SelectedItem = selectedRow.Cells["DoiTuong"].Value.ToString();
                cbxThoiGian.SelectedItem = selectedRow.Cells["ThoiGian"].Value.ToString();
                cbxLoaiVe.SelectedItem = selectedRow.Cells["LoaiVe"].Value.ToString();
                txtGiaVe.Text = selectedRow.Cells["Gia"].Value.ToString();
                lbMaVe.Text = selectedRow.Cells["MaVe"].Value.ToString();
            }
            else
            {
                btnSuaVe.Enabled = false;
            }

        }
        private void resetControl()
        {
            cbxDoiTuong.Text = "";
            cbxThoiGian.Text = "";
            cbxLoaiVe.Text = "";
            txtGiaVe.Text = "";
            lbMaVe.Text = "";
        }
        private void GridLoad()
        {
            gvVe.DataSource = new VeBUS().XemDanhSachVe();

        }

        private void btnHuy_Click(object sender, EventArgs e)
        {

            btnSuaVe.Enabled = false;
            gvVe.ClearSelection();
            gvVe.CurrentCell = null;
            resetControl();
        }

    }
}
